<template>
    <view class="containers">
		<!-- <view style="    width: 100px;
		text-align: center;
		background: #FF5722;
		color: #fff;
		border-radius: 20rpx;
		height: 50rpx;
		line-height: 50rpx;
		float: right;
		margin-right: 40rpx;">示例视频</view> -->
		<view class="content" >
			<view class="title">发布类型</view>
			<view class="inp" @click="task_statushow = true" style="display: flex;">
				<view class="div" >
				{{category_name}}
				</view>
				<view class="router" style="margin-top: 10rpx;">
					<u-icon name="arrow-right" color="#999"></u-icon>
				</view>
				<u-select 
					v-model="task_statushow" 
					mode="single-column" 
					:list="categoryList" 
					@confirm="categoryConfirm"
				></u-select>
			</view>

			<!-- <view class="title">任务类型</view>
			<view class="inp" @click="task_statushow = true" style="display: flex;">
				<view class="div" >
				{{category_name}}
				</view>
				<view class="router" style="margin-top: 10rpx;">
					<u-icon name="arrow-right" color="#999"></u-icon>
				</view>
				<u-select 
					v-model="task_statushow" 
					mode="single-column" 
					:list="categoryList" 
					@confirm="categoryConfirm"
				></u-select>
			</view> -->
			<view v-if="type == 1">
				<image src="https://oa.yuxiang123.cn/uploads/img/shiping.png" mode="aspectFill" class="shiping" @click="wenhao(6)"></image>
				<view class="title">标题</view>
				<view class="inp">
					<input type="text" v-model="title" placeholder="请输入标题">
				</view>
				<view class="title">内容</view>
				<view class="inp">
					<textarea placeholder="请输入内容..." v-model="content" maxlength="800"></textarea>
				</view>
				<view class="title">话题</view>
				<view class="inp">
					<textarea placeholder="请选择或输入#后编辑话题..." v-model="topic" maxlength="400" style="height:200rpx"></textarea>
					<view class="ht" @click="add_top()">#话题</view>
				</view>
				<view class="bom">
					<view class="bom_img">
						<view class="item" v-for="(item,index) in fileList" :key="index"   :class="itesm" >
							<image :src="item" mode="aspectFill" @click="clickImg(fileList,item)"></image>
							<u-icon class="icon" name="close-circle-fill" @click="delImg(index)"></u-icon>
						</view>
						
						<view class="item" v-if="fileList.length < 6"  @tap="chooseVideoImage(1)">
							<view class="item_icon">
								<u-icon name="plus"></u-icon>
							</view>
							<view class="item_text">上传封面图</view>
						</view>
					</view>
					<view class="bom_img" style="margin-top: 20rpx;">
						<view class="inp" style="width: 100%;">
							<input type="text" v-model="img_title" style="margin:0;height: 70rpx;width: 97%;" placeholder="可选(输入智能生图内文字)">
						</view>
					</view>
					
				</view>
				<view class="grid" v-if="id==0" >
					<view class="clo" @click="send_ai()" style="border: 3rpx solid #4CAF50;">AI生成</view>
					<view><image src="../../static/wenhao.svg" class="clo_img"  @click="wenhao(4)" ></image></view>
					<view class="inp" style="width: 60%;margin-left: 7%;display: flex;" @click="ai_statushow = true">
						<view class="div" style="text-align:right" >
						{{ai_name}}
						</view>
						<view class="router" style="margin-top: 10rpx;">
							<u-icon name="arrow-right" color="#999"></u-icon>
						</view>
						<u-select 
							v-model="ai_statushow" 
							mode="single-column" 
							:list="ainumList" 
							@confirm="aiConfirm"
						></u-select>
						
					</view>
					<!-- <view class="clo" @click="add_drafts(1)" style="margin-left: 9%;background-color: #9C27B0;">加草稿箱</view>
					<view><image src="../../static/wenhao.svg" class="clo_img"  @click="wenhao(5)" ></image></view> -->
				</view>
			</view>
			<view v-if="type == 2">
				<image src="https://oa.yuxiang123.cn/uploads/img/shiping.png" mode="aspectFill" class="shiping" @click="wenhao(6)"></image>
				<view class="title">标题</view>
				<view class="inp">
					<input type="text" v-model="title" placeholder="请输入标题">
				</view>
				<view class="title">内容</view>
				<view class="inp">
					<textarea placeholder="请输入内容..." v-model="content" maxlength="800"></textarea>
				</view>
				<view class="title">话题</view>
				<view class="inp">
					<textarea placeholder="请选择或输入#后编辑话题..." v-model="topic" maxlength="400" style="height:200rpx"></textarea>
					<view class="ht" @click="add_top()">#话题</view>
				</view>
				<view class="bom">
					<view class="bom_img">
						<view class="item" v-for="(item,index) in fileList" :key="index"   :class="itesm" >
							<image :src="item" mode="aspectFill" @click="clickImg(fileList,item)"></image>
							<u-icon class="icon" name="close-circle-fill" @click="delImg(index)"></u-icon>
						</view>
						
						<view class="item" v-if="fileList.length < 6"  @tap="chooseVideoImage(1)">
							<view class="item_icon">
								<u-icon name="plus"></u-icon>
							</view>
							<view class="item_text">选择图片</view>
						</view>
					</view>
					<view class="bom_img" style="margin-top: 20rpx;">
						<view class="clo" @click="add_img(1)" style="background-color: #ff9800;font-size: 30rpx;">智能生图</view>
						<view class="inp" style="width: 75%;">
							<input type="text" v-model="img_title" style="margin:0;height: 70rpx;margin-left: 5%;width: 92%;" placeholder="可选(输入智能生图内文字)">
						</view>
					</view>
					
				</view>
				<!-- <view class="grid" v-if="id==0" >
					<view class="clo" @click="send_ai()" style="border: 3rpx solid #4CAF50;">AI生成</view>
					<view><image src="../../static/wenhao.svg" class="clo_img"  @click="wenhao(4)" ></image></view>
					<view class="inp" style="width: 60%;margin-left: 7%;display: flex;" @click="ai_statushow = true">
						<view class="div" style="text-align:right" >
						{{ai_name}}
						</view>
						<view class="router" style="margin-top: 10rpx;">
							<u-icon name="arrow-right" color="#999"></u-icon>
						</view>
						<u-select 
							v-model="ai_statushow" 
							mode="single-column" 
							:list="ainumList" 
							@confirm="aiConfirm"
						></u-select>
						
					</view>
				</view> -->
			</view>
			<view v-if="is_ai">
				<view  v-for="(vo,i) in ai_list" :key="i"   >
					<view style="width: 98%;margin-left: 1%;border: 1rpx solid #d4d2d2;margin-top: 30rpx;"></view>
					<view class="biaotis">{{i+1}}</view>
					<view class="title" >标题</view>
					<view class="inp">
						<input type="text" v-model="ai_list[i]['title']" placeholder="请输入标题">
					</view>
					<view class="title">内容</view>
					<view class="inp">
						<textarea placeholder="请输入内容..." v-model="ai_list[i]['content']" maxlength="800"></textarea>
					</view>
					<view class="bom" id="the-id">
						<view class="bom_img">
							<view class="item" v-for="(item,index) in vo.img" :key="index" :class="ai_list[i]['class']">
								<image :src="item" mode="aspectFill" @click="clickImg('',item)"></image>
								<u-icon class="icon" name="close-circle-fill" @click="ai_delImg(i,index)"></u-icon>
							</view>
							<view class="item" v-if="vo.img.length < 6"  @tap="itemChooseVideoImage(2,i)">
								<view class="item_icon">
									<u-icon name="plus"></u-icon>
								</view>
								<view class="item_text">选择图片</view>
							</view>
						</view>
						<view class="bom_img" style="margin-top: 20rpx;">
							<view class="clo" @click="item_add_img(i,index)" style="background-color: #ff9800;font-size: 30rpx;">智能生图</view>
							<view class="inp">
								<input type="text" v-model="ai_list[i]['ai_img_title']" style="margin:0;height: 70rpx;margin-left: 30rpx;width: 130%;" placeholder="可选(输入图片内文字)">
							</view>
						</view>
						
					</view>
				</view>
				
				
				<!-- <view class="grid" v-if="id==0">
					<view class="clo" @click="add_drafts(2)" style="margin-left: 55%;background-color: #9C27B0;">加草稿箱</view>
				</view> -->
			</view>
			<view  v-if="type == 3">
				
				<view class="title">帖子链接</view>
				<view class="inp">
					<textarea placeholder="请输入帖子链接..." v-model="title" maxlength="600"></textarea>
				</view>
				<view class="title">评论内容</view>
				<view class="inp">
					<textarea placeholder="请输入评论内容..." v-model="content" maxlength="400" style="height:200rpx"></textarea>
				</view>
				<view class="title">评论图片 <text style="color: red;font-size: 30rpx;">(选填)</text>  </view>
				<view class="bom">
					<view class="bom_img">
						<view class="item" v-for="(item,index) in fileList" :key="index">
							<image :src="item" mode="aspectFill"></image>
							<u-icon class="icon" name="close-circle-fill" @click="delImg(index)"></u-icon>
						</view>
						<view class="item" v-if="fileList.length < 6"  @tap="chooseVideoImage(1)">
							<view class="item_icon">
								<u-icon name="plus"></u-icon>
							</view>
							<view class="item_text">选择图片</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="position: fixed;width: 100%;bottom: 0;height: 190rpx;background: #fff;z-index: 10000;">
			<view class="btn" @click="issue"  v-if="id==0">发布任务</view>
			<view class="btn" @click="issue_1"  v-if="id>0">确认编辑</view>

		</view>
		<view v-if="videoplay" style="position: absolute;
        top: -3000px;">
            <video
            controls
            id="myvideo"
            :src="videoUrl"
            autoplay="true"
            ></video>
        </view>
		<view class="index-con">
			<!-- 需要滚动的内容 放在一个根元素下 -->
			</view>
    </view>
</template>

<script>
export default {
		data() {
			return {
				ai_statushow:false,
				ainumList:[{value: '1',label: '1篇文章'},{value: '2',label: '2篇文章'},{value: '5',label: '5篇文章'},{value: '10',label: '10篇文章'}],
				ai_name:'1篇文章',
				ai_num: '1',
				itesm: 'dan',
				task_statushow: false,
				category_name:'AI智能发布文章',
				categoryList: [
					{
						value: '1',
						label: 'AI智能发布文章'
					},
					{
						value: '2',
						label: '普通发布文章'
					}
				],
				index:'',
				ai_fileList:[],
				ai_title:'',
				img_title:'',
				img_title_new:'',
				ai_img_title:'',
				ai_img_title_new:'',
				topic:'',
				ai_content:'',
                ai_list:[],
                fileList:[],
				videoplay:false,
				videoUrl:'',
				title:'',
				type:'1',
				content:'',
				id:0,
				is_tishiDetail:false,
				is_fb:false,
				is_ai:false,
				showCoupon:false,
			}
		},
		onLoad(p) {
			if(p.type){
				this.type = p.type;
			}
			if(p.drafts_id){
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/shop/getTaskDrafts',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					method: 'POST',
					data:{
						token:uni.getStorageSync('token'),
						id:p.drafts_id
					},
					success: (res) => {
						this.title = res.data.data.title;
						this.content = res.data.data.content;
						this.topic = res.data.data.topic;
						if(res.data.data.imgs){
							this.fileList = res.data.data.imgs_arr;
						}
						
					}
				})
			}

			//判断是否是华为
			let isHuaWei= getApp().globalData.isHuaWei;
			if(isHuaWei){
				this.setData({
					isHuaWei: isHuaWei
				})
			}
			this.userInfo = uni.getStorageSync('userInfo'); 

			if(p.id){
				this.id = p.id;
				this.getDetail();
			}
			//判断今天是否已经弹过一次
			uni.getStorage({
				key: "popUp-time"+this.userInfo.id,
				success: (res) => {
					if (!res.data || new Date().getTime() - res.data > 1000 * 60*60*24) {
						uni.setStorage({
							key: "popUp-time"+this.userInfo.id,
							data: new Date().getTime(),
						});
						this.showCoupon = true;
					}else{
						this.showCoupon = false;
					}
				},
				fail: (err) => {
					this.showCoupon = true;
				},
			});
		},
		onShow() {
			
		},
		onReady: function () {
			this.videoContext = uni.createVideoContext("myvideo", this);    // this这个是实例对象  必传
		},
		methods: {
			
			aiConfirm:function(e){
				this.ai_name = e[0].label;
				this.ai_num = e[0].value;
			},
			categoryConfirm:function(e){
				this.category_name = e[0].label;
				this.type = e[0].value;
			},
			clickImg(url,i) {
				
				var that = this;
				// #ifdef MP-WEIXIN
				if(url){
					uni.previewImage({
						current:i,
						urls: url
					})
				}else{
					uni.previewImage({
						urls: [i]
					})
				}
				// #endif
			},
			add_top(){
				this.topic = this.topic + '#';
			},
			item_add_img(i,index){
				var img = '';
				if(this.fileList[0]){
					img = this.fileList[0];
					if (img.includes('invite')) {
						if(this.img_title_new){
							img = this.img_title_new;
						}else{
							img = '';
						}
					}else{
						this.img_title_new = img;
					}
				}
				var that = this;
				var text = that.ai_list[i].ai_img_title;
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/index/getImgs',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					data:{
						text:text,
						img:img,
						num:1
					},
					method: 'POST',
					success: (res) => {
						if(res.data.code == 1){
							that.ai_list[i].img = res.data.data.new_url;
						}
					}
				})
			},
			add_img(type){
				var that = this;
				var img = '';
				if(type == 1){
					var text = that.img_title;
					if(this.fileList[0]){
						img = this.fileList[0];
						if (img.includes('invite')) {
							if(this.img_title_new){
								img = this.img_title_new;
							}else{
								img = '';
							}
						}else{
							this.img_title_new = img;
						}
					}else{
						this.img_title_new = '';
					}
				}else{
					var text = that.ai_img_title;
					if(this.ai_fileList[0]){
						img = this.ai_fileList[0];
						if (img.includes('invite')) {
							if(this.ai_img_title_new){
								img = this.ai_img_title_new;
							}else{
								img = '';
							}
						}else{
							this.ai_img_title_new = '';
						}
					}else{
						this.ai_img_title_new = '';
					}
				}
				
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/index/getImgs',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					data:{
						text:text,
						img:img,
						num:6
					},
					method: 'POST',
					success: (res) => {
						if(res.data.code == 1){
							if(type == 1){
								that.fileList = res.data.data.new_url;
								if(that.fileList.length > 1){
									that.itesm = 'shuang';
								}else{
									that.itesm = 'dan';
								}
							}else{
								that.ai_fileList = res.data.data.new_url;
							}

							
						}
					}
				})
			},
			wenhao(type){
				// uni.showModal({
				// 	title: 'AI使用说明', // 标题
				// 	content: 'Ai系统将根据你提供的标题，文案，生成表达意思近似的新文案和内容，生成后，你可以直接提交使用或者修改后再使用。', // 内容(灰色字体)
				// 	showCancel: true, // 显示取消按钮
				// 	confirmText: '确定', // 确定按钮文字
				// 	confirmColor: '#007aff', // 确定按钮颜色
				// 	success: (res) => {
				// 	}
				// })

				var that = this;
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/index/get_noviceguide',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					data:{
						type:type
					},
					method: 'POST',
					success: (res) => {
						if(res.data.code == 1){
							var data = res.data.data;
							//e 是通过点击事件拿到的视屏地址
							that.videoUrl =data[0].video;
							that.videoContext.requestFullScreen({ direction: 0 });  //direction: 90  这个是控制全屏的时候视屏旋转多少度 
							that.videoContext.play();
							that.videoplay = true;  // 显示播放盒子
						}
					}
				})
			},
			screenChange(e){
				let fullScreen = e.detail.show; //值true为进入全屏，false为退出全屏
				if (!fullScreen) {
					//退出全屏
					this.videoplay = false;   // 隐藏播放盒子
					this.videoContext.pause();
				}
			},
			send_ai(){
				var that = this;
				if(that.userInfo.shop.ai_num > 0){
					that.get_ai();
				}else{
					if(that.showCoupon == true){
						uni.showModal({
							title: 'AI计费标准', // 标题
							content: '单次消费¥0.05/次。可购套餐：¥4.8/100次、¥23.8/500次、¥46.8/1000次、¥228/5000次', // 内容(灰色字体)
							showCancel: true, // 显示取消按钮
							cancelText: '购买套餐', // 取消按钮文字
							cancelColor: '#000000', // 取消按钮颜色
							confirmText: '直接使用', // 确定按钮文字
							confirmColor: '#007aff', // 确定按钮颜色
							success: (res) => {
								if (res.confirm) {
									that.get_ai();
								} else {
									uni.navigateTo({
										url:'/merchant/shop/ai_recharge'
									})	
								}
							}
						})
						that.closeCouponNew();
					}else{
						that.get_ai();
					}
				}
			},
			// 关闭弹框
			closeCouponNew(){
				this.showCoupon = false;
				//存key以及时间
				uni.setStorage({
					key: "popUp-time"+this.userInfo.id,
					data: new Date().getTime(),
				});
			},
			get_ai(){
				var that = this;
				if(!(that.ai_num > 0 && that.ai_num < 11)){
					return that.$Toast('一次生成的文章不可超过10篇');
				}
				if(!that.title && !that.content){
					return that.$Toast('标题和内容不可同时为空')
				}
				uni.showLoading({
					title: 'AI生成数据中..'
				})
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/index/getMoonshotAi',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					method: 'POST',
					data:{
						user_id:that.userInfo.id,
						shop_id:that.userInfo.shop.id,
						title:that.title,
						content:that.content,
						ai_num:that.ai_num,
						img_title:that.img_title,
						img:that.fileList.length > 0 ? that.fileList[0] : ''
					},
					success: (res) => {
						that.scrollToBottom();
						uni.hideLoading();
						if(res.data.code == 1){
							that.is_ai = true;
							for (var i = 0; i < res.data.data.length; i++) {
								res.data.data[i].content = res.data.data[i].content+that.topic;
							}
							that.ai_list =  res.data.data;
							//that.ai_title = res.data.data.title;
							//that.ai_content = res.data.data.content+that.topic;
							that.$Toast(res.data.msg);
						}else{
							if(res.data.msg == '账户余额不足'){
								uni.showModal({
									title: '您的账户余额不足，是否去充值?', // 标题
									content: '', // 内容(灰色字体)
									showCancel: true, // 显示取消按钮
									cancelText: '取消', // 取消按钮文字
									cancelColor: '#000000', // 取消按钮颜色
									confirmText: '去充值', // 确定按钮文字
									confirmColor: '#007aff', // 确定按钮颜色
									success: (res) => {
										if (res.confirm) {
											uni.navigateTo({
												url:'/merchant/shop/recharge'
											})	
										} else {
											
										}
									}
								})
							}else{
								that.$Toast(res.data.msg);
							}
						}
					}
				})
			},
			add_drafts(type){
				if(type == 2){
					var title = this.ai_title;
					var content = this.ai_content;
					var fileList = this.ai_fileList;
				}else{
					var title = this.title;
					var content = this.content;
					var fileList = this.fileList;
				}
				if(title == ''){
					return this.$Toast('标题不可为空!')
				}
                if(content == ''){
					return this.$Toast('内容不可为空!')
				}

				var imgs = '';
				for(var i=0;i<fileList.length;i++){
					if(i+1==fileList.length){
						imgs += fileList[i]
					}else{
						imgs += fileList[i] + ','
					}
				}
				uni.request({
                	url: 'https://oa.yuxiang123.cn/api/shop/addShopTaskDrafts',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					method: 'POST',
					data:{
						token:uni.getStorageSync('token'),
						title:title,
						content:content,
						imgs:imgs
					},
					success: (res) => {
						this.$Toast(res.data.msg);
					}
				})
			},
			getDetail:function(){
				uni.request({
                	url: 'https://oa.yuxiang123.cn/api/shop/getInfo',
					header: {
						"content-type": "application/x-www-form-urlencoded"
					},
					method: 'POST',
					data:{
						token:uni.getStorageSync('token'),
						id:this.id,
						type:1
					},
					success: (res) => {
						if(res.data.code == 1){
							this.title = res.data.data.title;
							this.content = res.data.data.content;
							this.topic = res.data.data.topic;
							if(res.data.data.imgs){
								this.fileList = res.data.data.imgs_arr;
							}
							
						}
					}
				})
			},
			itemChooseVideoImage(type,i) {
                this.selectImg(9,type,i);
			},

			//点击上传图片或视频
			chooseVideoImage(type) {
				var that = this;
                if(!uni.getStorageSync('get_image_issueDetail') && that.isHuaWei){
                    uni.showModal({
                        title: "获取你的相册/相机权限",
                        content: "将获取你的相册/相机权限,用于发布任务",
                        success: function (res) {
                            if (res.cancel) {
                            } else if (res.confirm) {
                                uni.setStorageSync('get_image_issueDetail',1);
                                that.selectImg(9,type);
                            }
                        },
                    });
                }else{
                    that.selectImg(9,type);
                }
			},
			issue_1(){
				var title = this.title;
				var content = this.content+this.topic;
				var fileList = this.fileList;
				if(title == ''){
					return this.$Toast('标题不可为空!')
				}
                if(content == ''){
					return this.$Toast('内容不可为空!')
				}
				if(fileList.length == 0 ){
					return this.$Toast('图片不可为空!')
				}
				var imgStr = '';
				for(var i=0;i<fileList.length;i++){
					if(i+1==fileList.length){
						imgStr += fileList[i]
					}else{
						imgStr += fileList[i] + ','
					}
				}

				var that = this;
				//发布之前验证图片和视频和文字是否违规
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/green/examine_img',
					header:{"content-type": "application/x-www-form-urlencoded"},
					data:{
						title:that.title+that.content,
						imgs:imgStr
					},
					method: 'POST',
					success: (res) => {
						if(res.data.code == 0 && res.data.msg == '存在违规'){
							this.$Toast('您的内容或图片涉及违规，请重新编辑');
							return false;
						}
						uni.request({
							url: 'https://oa.yuxiang123.cn/api/shop/shop_add_task',
							header:{"content-type": "application/x-www-form-urlencoded"},
							data:{
								title:title,
								content:content,
								imgs:imgStr,
								token:uni.getStorageSync('token'),
								id:that.id
							},
							method: 'POST',
							success: (res) => {
								that.$Toast('操作成功')
								if(res.data.code == 1){
									setTimeout(function () {
										//返回上一页
										uni.navigateBack({
											delta:1
										})
									}, 600);

									
								}
							}
						})
					}
				})
			},
			issue(){
				var that = this;
				//ai
				if(this.is_ai){
					var info =  JSON.stringify(this.ai_list);
					uni.request({
						url: 'https://oa.yuxiang123.cn/api/shop/more_shop_add_task',
						header:{"content-type": "application/x-www-form-urlencoded"},
						data:{
							info:this.ai_list,
							type:that.type,
							token:uni.getStorageSync('token'),
						},
						method: 'POST',
						success: (res) => {
							if(res.data.code == 1){
								var title = '您已发布成功,是否继续发布新任务?';
								var cancelText = '编辑原稿';
								var confirmText = '继续生成';
								uni.showModal({
									title: title, // 标题
									content: '', // 内容(灰色字体)
									showCancel: true, // 显示取消按钮
									cancelText: cancelText, // 取消按钮文字
									cancelColor: '#000000', // 取消按钮颜色
									confirmText: confirmText, // 确定按钮文字
									confirmColor: '#007aff', // 确定按钮颜色
									success: (res) => {
										if (res.confirm) {
											that.is_ai = false;
											that.ai_list = [];
											// if(that.is_ai){
											// 	that.ai_title = '';
											// 	that.ai_content = '';
											// 	that.is_ai = false;
											// 	that.ai_fileList = [];
											// 	that.get_ai();
											// }else{
											// 	that.title = '';
											// 	that.content = '';
											// 	that.fileList = [];
											// 	that.topic = '';
											// }
										} else {
											that.is_ai = false;
											that.ai_list = [];
											// uni.switchTab({
											// 	url:'/pages/shop/shopClass'
											// })	
										}
									}
								})
							}else{
								if(res.data.msg == '账号余额不足'){
									uni.showModal({
										title: '您的账户余额不足，是否去充值?', // 标题
										content: '', // 内容(灰色字体)
										showCancel: true, // 显示取消按钮
										cancelText: '取消', // 取消按钮文字
										cancelColor: '#000000', // 取消按钮颜色
										confirmText: '去充值', // 确定按钮文字
										confirmColor: '#007aff', // 确定按钮颜色
										success: (res) => {
											if (res.confirm) {
												uni.navigateTo({
													url:'/merchant/shop/recharge'
												})	
											} else {
												
											}
										}
									})
								}else{
									that.$Toast(res.data.msg);
								}
							}
						}
					})
					
					return;




					var title = this.ai_title;
					var content = this.ai_content;
					var fileList = this.ai_fileList;

				}else{

					var title = this.title;
					var content = this.content+this.topic;
					var fileList = this.fileList;
				}
				if(title == ''){
					return this.$Toast('标题不可为空!')
				}
                if(content == ''){
					return this.$Toast('内容不可为空!')
				}
				if(fileList.length == 0 && this.type == 1){
					return this.$Toast('图片不可为空!')
				}

				var imgStr = '';
				for(var i=0;i<fileList.length;i++){
					if(i+1==fileList.length){
						imgStr += fileList[i]
					}else{
						imgStr += fileList[i] + ','
					}
				}
				
				var that = this;
				//发布之前验证图片和视频和文字是否违规
				uni.request({
					url: 'https://oa.yuxiang123.cn/api/green/examine_img',
					header:{"content-type": "application/x-www-form-urlencoded"},
					data:{
						title:title+content,
						imgs:imgStr
					},
					method: 'POST',
					success: (res) => {
						if(res.data.code == 0 && res.data.msg == '存在违规'){
							that.$Toast('您的内容或图片涉及违规，请重新编辑');
							return false;
						}
						if(that.is_fb){
							that.$Toast('请勿重复点击发布');
							return false;
						}
						that.is_fb = true;
						uni.request({
							url: 'https://oa.yuxiang123.cn/api/shop/shop_add_task',
							header:{"content-type": "application/x-www-form-urlencoded"},
							data:{
								title:title,
								content:content,
								imgs:imgStr,
								type:that.type,
								token:uni.getStorageSync('token'),
							},
							method: 'POST',
							success: (res) => {
								setTimeout(function () {
									that.is_fb = false;
								}, 1000);
								if(res.data.code == 1){
									if(that.is_ai){
										var title = '您已发布成功,是否继续发布新任务?';
										var cancelText = '编辑原稿';
										var confirmText = '继续生成';
									}else{
										var title = '您已发布成功,是否继续发布新任务?';
										var cancelText = '回到首页';
										var confirmText = '继续发布';
									}
									uni.showModal({
										title: title, // 标题
										content: '', // 内容(灰色字体)
										showCancel: true, // 显示取消按钮
										cancelText: cancelText, // 取消按钮文字
										cancelColor: '#000000', // 取消按钮颜色
										confirmText: confirmText, // 确定按钮文字
										confirmColor: '#007aff', // 确定按钮颜色
										success: (res) => {
											if (res.confirm) {
												if(that.is_ai){
													that.ai_title = '';
													that.ai_content = '';
													that.is_ai = false;
													that.ai_fileList = [];
													that.get_ai();
												}else{
													that.title = '';
													that.content = '';
													that.fileList = [];
													that.topic = '';
												}
											} else {
												if(that.is_ai){
													that.ai_title = '';
													that.ai_content = '';
													that.ai_fileList = [];
													that.is_ai = false;
												}else{
													uni.switchTab({
														url:'/pages/shop/shopClass'
													})	
												}
												
											}
										}
									})
								}else{
									if(res.data.msg == '账号余额不足'){
										uni.showModal({
											title: '您的账户余额不足，是否去充值?', // 标题
											content: '', // 内容(灰色字体)
											showCancel: true, // 显示取消按钮
											cancelText: '取消', // 取消按钮文字
											cancelColor: '#000000', // 取消按钮颜色
											confirmText: '去充值', // 确定按钮文字
											confirmColor: '#007aff', // 确定按钮颜色
											success: (res) => {
												if (res.confirm) {
													uni.navigateTo({
														url:'/merchant/shop/recharge'
													})	
												} else {
													
												}
											}
										})
									}else{
										that.$Toast(res.data.msg);
									}
								}
							}
						})
					}
				})
			},
			/**
			 * @author gongliying
			 * @date 2019-07-26
			 * @information 跳转页面底部
			 */
			scrollToBottom(){
				// 获取页面内容高度
				// 设置滚动条位置为页面内容高度，即到达页面最底部
				// uni.pageScrollTo({
				// 	selector: '#the-id',
				// 	duration: 300 // 过渡时间（单位ms）
				// });

				const query = uni.createSelectorQuery()
				query.select('.index-con').boundingClientRect((data) => {
					console.log(data, 'data')
					let pageScrollTop = Math.round(data.top)
					uni.pageScrollTo({
						scrollTop: pageScrollTop, //滚动的距离
						duration: 0, //过渡时间
					})
				}).exec();
			},
			setnotice(){
				uni.request({
					url:'https://oa.yuxiang123.cn/api/order/product_notice',
					method:'POST',
					header:{"content-type":"application/x-www-form-urlencoded"},
					data:{'product_id':this.product_id,token:uni.getStorageSync('token'),'notice':this.on},
				})
			},
			selectImg(index,type,i){
				var that = this;
				// #ifdef  APP-PLUS
				uni.chooseImage({
						count: 9, //默认9
						sizeType: ['original'],
						crop:{
							width:300,
							height:400
						},
						success: function (res) {
                            for (let index = 0; index < res.tempFilePaths.length; index++) {
                                that.upImg(res.tempFilePaths[index],'index',type,i)
                            }
						},
				});
 				// #endif
				// #ifdef MP-WEIXIN
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original'],
					success: function (res) {
                        for (let index = 0; index < res.tempFilePaths.length; index++) {
                            that.upImg(res.tempFilePaths[index],'index',type,i)
                        }
					}
				});
				// #endif
			},
			upImg:function(url,index,type,i){
				this.$show();
				var that = this;
				uni.uploadFile({
					url: 'https://oa.yuxiang123.cn/api/common/upload',
					filePath: url,
					name: 'file',
					success: (uploadFileRes) => {
						that.$hide()
						var data = JSON.parse(uploadFileRes.data);
						if(data.code == 1){
							if(type == 1){
								that.fileList.push(data.data.fullurl)
								if(that.fileList.length > 1){
									that.itesm = 'shuang';
								}else{
									that.itesm = 'dan';
								}
							}else{
								that.ai_list[i].img.push(data.data.fullurl)
								if(that.ai_list[i].img.length > 1){
									that.ai_list[i].class = 'shuang';
								}else{
									that.ai_list[i].class = 'dan';
								}
								//that.ai_fileList.push(data.data.fullurl)
							}
						}else{
							uni.showToast({
								icon: 'none',
								title: '图片上传失败,换张图试试'
							})
						}
					}
				});
			},
			delImg(index){
				this.fileList.splice(index,1)
				if(this.fileList.length > 1){
					this.itesm = 'shuang';
				}else{
					this.itesm = 'dan';
				}
				
			},
			ai_delImg(i,index){
				this.ai_list[i]['img'].splice(index,1);
				if(this.ai_list[i].img.length > 1){
					this.ai_list[i].class = 'shuang';
				}else{
					this.ai_list[i].class = 'dan';
				}
				
			}
		}
	}
</script>
<style>
	page{
		background-color: #f2f9fd;
		
	}
</style>
<style lang="scss">
	.biaotis{
		width: 44rpx;
		height: 40rpx;
		text-align: center;
		line-height: 40rpx;
		background: red;
		color: #fff;
		border-radius: 50%;
		margin-top: 20rpx;
	}
	.ht{
		width: 120rpx;
		height: 50rpx;
		text-align: center;
		line-height: 50rpx;
		border-radius: 20rpx;
		background-color: #f7f7f7;
		font-size: 26rpx;   
		margin-top: -18rpx;
		margin-bottom: 18rpx;
	}
	.containers{
		padding-bottom: 300rpx;
	}
    .top_title{
        z-index: 99999;
        background: #f2f9fd;
        width: 100%;
        height: 50rpx;
        padding-top: 100rpx;
        padding-bottom: 15rpx;
        text-align: center;
    }
    
    .back{
        position: absolute;
        left: 25rpx;
        width: 45rpx;
        height: 45rpx;
    }
	.content{
		margin-top: 50rpx;
		width: 86%;
		margin-left: 3%;
		background-color: #fff;
		border-radius: 40rpx;
		padding: 4%;
	}
	.shiping{
		width: 90rpx;
    height: 90rpx;
    position: fixed;
    right: 50rpx;
    top: 50rpx;
    z-index: 100000;
	}
	.title{
		font-size: 38rpx;
		font-weight: 550;
		margin: 10rpx 0;
	}
	.inp input {
		margin: 20rpx 0;
		width: 97%;
		height: 88rpx;
		flex-shrink: 0;
		border-radius: 8px;
		background: #f1f4fe;
		text-align: left;
		padding-left: 3%;
	}
	.inp textarea {
		margin: 20rpx 0;
		width: 94%;
		height: 420rpx;
		flex-shrink: 0;
		border-radius: 8px;
		background: #f1f4fe;
		text-align: left;
		padding: 3%;
	}
	.bom_img{
		display: flex;
		flex-wrap: wrap;
		.item{
			text-align: center;
			width: 30%;
			background-color: #f1f4fe;
			height: 200upx;
			border-radius: 10upx;
			margin-top: 20upx;
			color: #999999;
			margin-right: 20upx;
			position: relative;
			.icon{
				position: absolute;
				right: -10upx;
				top: -20upx;
				font-size: 40upx;
				background-color: #FFFFFF;
				border-radius: 50%;
				color: rgba($color: #333, $alpha: 0.5);
			}
			.item_icon{
				margin-top: 40upx;
				font-size: 40upx;
			}
			.item_text{
				color: #999999;
				font-size: 26upx;
				margin-top: 10upx;
			}
			image{
				width: 100%;
				height: 100%;
				border-radius: 10upx;
			}
			.seturl{
				position: absolute;
				bottom: 0;
				width: 100%;
				background-color: rgba($color: #333, $alpha: 0.5);
				border-radius: 0 0 10upx 10upx;
				color: #FFFFFF;
				font-size: 24upx;
				padding: 10upx 0;
			}
			.selectSet{
				background-color: rgba($color: red, $alpha: 0.5);
			}
		}
	}
	.grid {
		display: flex;
		margin-top: 40rpx;
		flex-wrap: wrap;
	}

	.grid .clo{
		width: 25%;
		background: #4CAF50;
		color: #fff;
		text-align: center;
		border-radius: 30rpx;
		height: 70rpx;
		line-height: 70rpx;
		font-size: 34rpx;
		
	}
	.bom .clo{
		width: 25%;
		background: #4CAF50;
		color: #fff;
		text-align: center;
		border-radius: 30rpx;
		height: 70rpx;
		line-height: 70rpx;
		font-size: 34rpx;
		
	}
	.clo_img{
		width: 45rpx;height: 45rpx;   position: absolute;
	}
	.btn{
		background-color: #3662ec;
		color: #fff;
		width: 90%;
		margin: auto;
		margin-top: 35upx;
		text-align: center;
		padding: 20upx 0;
		font-size: 34upx;
		border-radius: 50upx;
	}
	.u-content{
		height:70upx !important;
	}
	.div{
		flex: 1;
		text-align: left;
		margin-right: 20upx;
		color:#999;
		height: 30px;
		margin-top: 10rpx;
	}
	.shuang{
		width: 46% !important;
		height: 320rpx !important;
	}

	.dan{
		width: 75% !important;
		height: 550rpx !important;
	}
</style>

